/*
 * @Author: master
 * @Date: 2022-04-06 14:29:46
 * @LastEditTime: 2022-04-12 11:33:59
 * @Description: 路由监听
 */
import mitt from 'mitt'; // 事件监听
const emitter = mitt();
const key = Symbol('ROUTE_CHANGE');
let latestRoute;
/**
 * 添加路由监听
 * @param {Object} to
 */
export function setRouteEmitter(to, from) {
  latestRoute = to;
  emitter.emit(key, to);
}
/**
 * 路由变化监听
 * @param {Function} handler
 * @param immediate
 */
export function listenerRouteChange(handler, immediate = true) {
  emitter.on(key, type => handler(type));
  if (immediate && latestRoute) {
    handler(latestRoute);
  }
}
/**
 * 删除路由监听
 */
export function removeRouteListener() {
  emitter.off(key);
}
